package de.baumann.browser.present

import de.baumann.browser.api.OdinObserver
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.IBindPhoneView
import de.baumann.browser.model.UserModel
import de.baumann.browser.utils.updateLoginname
import de.baumann.browser.utils.updateUserPhone

class BindPhonePresenter : BasePresenter<IBindPhoneView>() {
    private val userModel: UserModel = UserModel()

    fun bindPhoneGetCode() {
        this.userModel.bindPhoneGetCode(view.oldPhone,
                view.telNum,
                view.password)
                .subscribe(object : OdinObserver<Any>(view) {
                    override fun onSuccess(data: Any?) {
                        view.let {
                            view.requestSuccess(-1)
                        }
                    }
                })
    }


    fun bindPhone() {
        this.userModel.bindPhone(view.telNum,
                view.smsCode,
                view.type)
                .subscribe(object : OdinObserver<Any>(view) {
                    override fun onSuccess(data: Any?) {
                        updateLoginname(view.telNum)
                        updateUserPhone(view.telNum)
                        if (view != null)
                            view.requestSuccess(0)
                    }
                })
    }

}